package net.oschina.bilbodai.common.retrofitor.core;

import okhttp3.OkHttpClient;
import org.springframework.beans.factory.FactoryBean;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;

import java.util.Set;

/**
 * @author daimao  Date: 16/4/14 Time: 下午3:36
 * @version $Id$
 */
public class RetrofitorFactoryBean implements FactoryBean {

    private boolean useDefaultConverters;
    private String baseUrl;
    private Class<?> targetType;
    private OkHttpClient okHttpClient;
    private Set<Converter.Factory> converterFactorySet;
    private Set<CallAdapter.Factory> adapterFactorySet;

    @Override
    public Object getObject() throws Exception {
        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl(baseUrl);
        if (okHttpClient != null) {
            builder.client(okHttpClient);
        }
        if (isUseDefaultConverters()) {
            builder.addConverterFactory(JacksonConverterFactory.create());
            builder.addConverterFactory(SimpleXmlConverterFactory.create());
        }
        if (converterFactorySet != null) {
            for (Converter.Factory factory : converterFactorySet) {
                builder.addConverterFactory(factory);
            }
        }
        if (adapterFactorySet != null) {
            for (CallAdapter.Factory factory : adapterFactorySet) {
                builder.addCallAdapterFactory(factory);
            }
        }
        Retrofit retrofit = builder.build();
        return retrofit.create(targetType);
    }

    @Override
    public Class<?> getObjectType() {
        return targetType;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public Class<?> getTargetType() {
        return targetType;
    }

    public void setTargetType(Class<?> targetType) {
        this.targetType = targetType;
    }

    public OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }

    public void setOkHttpClient(OkHttpClient okHttpClient) {
        this.okHttpClient = okHttpClient;
    }

    public Set<Converter.Factory> getConverterFactorySet() {
        return converterFactorySet;
    }

    public void setConverterFactorySet(Set<Converter.Factory> converterFactorySet) {
        this.converterFactorySet = converterFactorySet;
    }

    public Set<CallAdapter.Factory> getAdapterFactorySet() {
        return adapterFactorySet;
    }

    public void setAdapterFactorySet(Set<CallAdapter.Factory> adapterFactorySet) {
        this.adapterFactorySet = adapterFactorySet;
    }

    public boolean isUseDefaultConverters() {
        return useDefaultConverters;
    }

    public void setUseDefaultConverters(boolean useDefaultConverters) {
        this.useDefaultConverters = useDefaultConverters;
    }
}